/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 14, 2012  11:16:16 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : MmPocontractorOutbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmPocontractorOutb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class MmPocontractorOutbServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(MmPocontractorOutbServices.class);

	/**
	 * @param em
	 */
	public MmPocontractorOutbServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public MmPocontractorOutb findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "MmPocontractorOutb.findById"));				
	}
	
	public List<MmPocontractorOutb> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "MmPocontractorOutb.findByInstanceNo"));				
	}
	
	public List<MmPocontractorOutb> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmPocontractorOutb.findAllIsSync", mapWhereValue));				
	}
	
	public List<MmPocontractorOutb> findByDocumentNo(String company, String project, String documentNo, String subStatus, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		mapWhereValue.put("subStatus", subStatus);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmPocontractorOutb.findByDocumentNo", mapWhereValue));				
	}
	
	public MmPocontractorOutb Updated(MmPocontractorOutb mmPocontractorOutb) throws Exception {
		if(mmPocontractorOutb != null)
		{
			return (MmPocontractorOutb) super.updated(mmPocontractorOutb);
		}else
			return null;
	}
	
	public MmPocontractorOutb InsertedOrUpdated(MmPocontractorOutb vlaue) throws Exception {
		MmPocontractorOutb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("MM_PURCHASE_ORDERS");					
				obj.setPromisStagingId(id);				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<MmPocontractorOutb> Inserted(List<MmPocontractorOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			if(!CommonService.IsNull(vlauelist))
			{
				log.info(" size :" + common_lst.size());
				for(MmPocontractorOutb obj : vlauelist)
				{
					log.info("getPromisStagingId : " +obj.getPromisStagingId() + " // DocumentNo : " + obj.getDocumentNo());
					if(CommonService.IsNull(obj.getPromisStagingId()))
					{
						String id = super.SequenceNext("MM_PURCHASE_ORDERS");
						log.info("Id : " +id);
						obj.setPromisStagingId(id);		
						obj.setSyncCreatedby(this.getUser());
						obj.setSyncCreatedon(this.getCurrent_date());
						obj.setSyncUpdatedby(this.getUser());
						obj.setSyncUpdatedon(this.getCurrent_date());
						obj.setIsactive("Y");
						obj.setIssync("N");
						common_lst.add(obj);
					}	
				}
				
				if(common_lst != null && common_lst.size() >= 1)
					super.inserted(common_lst);
			}			
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<MmPocontractorOutb> Updated(List<MmPocontractorOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmPocontractorOutb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
}
